package florestapokemon;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class Intermediation {

	private HashMap<String, Position> target;
	private HashMap<Position, Pokemon> map;
	private int pokeLimit;
	private int pokemonNum;
	private int maxX;
	private int maxY;

	public Intermediation(){

		maxX=maxY=2;
		pokeLimit = 20;
		pokemonNum = 5;
		map = new HashMap<Position, Pokemon>();
		target = new HashMap<String, Position>();

		for(int i=0; i<7 ; ++i){

			Pokemon p = new Pikachu();

			switch(new Random().nextInt(pokemonNum)){
			case 0: p = new Pikachu();
			break;
			case 1: p = new Bulbasaur();
			break;
			case 2: p = new Squirtle();
			break;
			case 3: p = new Geodude();
			break;
			case 4: p = new Charmander();
			break;
			}
			map.put(new Position(new Random().nextInt(maxX), new Random().nextInt(maxY)),p);
		}
	}

	public void initTargets(){
		target.put("Pikachu", new Position(new Random().nextInt(maxX), new Random().nextInt(maxY)));
		target.put("Bulbasaur", new Position(new Random().nextInt(maxX), new Random().nextInt(maxY)));
		target.put("Squirtle", new Position(new Random().nextInt(maxX), new Random().nextInt(maxY)));
		target.put("Charmander", new Position(new Random().nextInt(maxX), new Random().nextInt(maxY)));
		target.put("Geodude", new Position(new Random().nextInt(maxX), new Random().nextInt(maxY)));
	}

	public void doIntermediation(){
		initTargets();

		ArrayList<Position> positions = new ArrayList<Position>(map.keySet());

		for (Position pos : positions) {
			if (!map.containsKey(pos))
				continue;

			Pokemon p = map.get(pos);		
			map.remove(pos);
			
			Position nextStep = pos.nextStep(target.get(p.getPokemonName()));
			map.put(nextStep, p);

			if(map.containsKey(nextStep)){

				if(!map.get(nextStep).getPokemonName().equals(p.getPokemonName())){
					map.get(p).attack(map.get(nextStep));
				}
				
				else if(!p.getPokemonName().equals(map.get(nextStep).getPokemonName())){
					p.attack(map.get(nextStep));
				}

				if(p.isPokemonDead()){
					map.remove(p);
					System.out.println("Um " + p.getPokemonName() +" foi morto");
				}else if (map.get(nextStep).isPokemonDead()){
					map.remove(nextStep);	
					System.out.println("Um " + map.get(nextStep).getPokemonName() + " foi morto.");
				}	
			}

			if(pos.equals(target.get(p))){
				
				System.out.println("Um " + p.getPokemonName() + " encontrou seu target.");
				
				target.put(p.getPokemonName(), new Position(new Random().nextInt(maxX), new Random().nextInt(maxY)));

				if(map.size() < pokeLimit){

					Pokemon ditto = new Pikachu();

					switch(new Random().nextInt(pokemonNum)){
					case 0: ditto = new Pikachu();
					break;
					case 1: ditto = new Bulbasaur();
					break;
					case 2: ditto = new Squirtle();
					break;
					case 3: ditto = new Geodude();
					break;
					case 4: ditto = new Charmander();
					break;
					}
					map.put(new Position(new Random().nextInt(maxX), new Random().nextInt(maxY)), ditto);
				}
			}
		}
	}

	public Map<Position, Pokemon> getState() {
		return map;
	}

}
